<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <th:block th:include="include :: header('合同离租')" />
    <th:block th:include="include :: datetimepicker-css" />
    <th:block th:include="include :: select2-css" />
    <th:block th:include="include :: bootstrap-select-css" />
</head>
<body class="white-bg">
    <div class="wrapper wrapper-content animated fadeInRight ibox-content">
        <form class="form-horizontal m" id="form-tenant-edit" th:object="${sysTenant}">
            <input name="tenantId" th:field="*{tenantId}" type="hidden">  <!--租户id-->
            <input name="endTime" th:value="${#dates.format(sysTenant.endTime, 'yyyy-MM-dd')}" placeholder="yyyy-MM-dd" type="hidden" >  <!--租户合同到期时间-->
            <div class="form-group" style="margin-top: 10%">
                <label class="col-sm-3 control-label is-required">离租情况：</label>
                <div class="col-sm-8">
                    <label class="radio-box"> <input type="radio" name="tenantStatus" value="2" checked/> 正常离租 </label>
                    <label class="radio-box"> <input type="radio" name="tenantStatus" value="1" /> 提前离租 </label>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label is-required">离租时间：</label>
                <div class="col-sm-6">
                    <div class="input-group date">
                        <span class="input-group-addon"><i class="fa fa-calendar"></i></span>
                        <input name="leaveTime" th:value="${#dates.format(sysTenant.endTime, 'yyyy-MM-dd')}" class="form-control" placeholder="yyyy-MM-dd" type="text" required>
                    </div>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label">备注：</label>
                <div class="col-sm-6">
                    <textarea name="remark" th:field="*{remark}" maxlength="500" class="form-control" rows="3" ></textarea>
                </div>
            </div>
        </form>
    </div>

    <div class="row">
        <div class="col-sm-offset-5 col-sm-10">
            <button type="button" class="btn btn-sm btn-primary" onclick="submitHandler()"><i class="fa fa-check"></i>保 存</button>&nbsp;
            <button type="button" class="btn btn-sm btn-danger" onclick="closeItem()"><i class="fa fa-reply-all"></i>关 闭 </button>
        </div>
    </div>

    <th:block th:include="include :: footer" />
    <th:block th:include="include :: datetimepicker-js" />
    <th:block th:include="include :: select2-js" />
    <th:block th:include="include :: bootstrap-select-js" />
    <script type="text/javascript">
        var prefix = ctx + "system/tenant";
        $("#form-tenant-edit").validate({
            focusCleanup: true
        });

        function submitHandler() {
            if ($.validate.form()) {
                var data = $('#form-tenant-edit').serializeArray();
                var endTime,tenantStatus,leaveTime,remark;
                for(var tmp in data) {
                    if(data[tmp].name == "endTime"){
                        endTime = data[tmp].value;
                    }
                    if(data[tmp].name == "tenantStatus"){
                        tenantStatus = data[tmp].value;
                    }
                    if(data[tmp].name == "leaveTime"){
                        leaveTime = data[tmp].value;
                    }
                    if(data[tmp].name == "remark"){
                        remark = data[tmp].value;
                    }
                }
                if(tenantStatus == '1'){
                    if(leaveTime == '' || leaveTime == null){
                        $.modal.alertWarning('离租时间不能为空');
                        return;
                    }
                    if(remark == '' || remark == null){
                        $.modal.alertWarning('备注不能为空');
                        return;
                    }
                }else {
                    if(leaveTime == '' || leaveTime == null){
                        for(var tmp in data) {
                            if(data[tmp].name == "leaveTime"){
                                data[tmp].value = endTime;
                                break;
                            }
                        }
                    }
                }
                $.operate.save(prefix + "/leaveSave", data);
            }
        }

        $('#unit').selectpicker({
            'width':'300px',
        });

        $("input[name='leaveTime']").datetimepicker({
            format: "yyyy-mm-dd",
            minView: "month",
            autoclose: true
        });

    </script>
</body>
</html>